package com.flower.domain;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.flower.enums.*;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
@Data
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 8004487252556526569L;
    //状态
    private int status;
    //是否成功
    private boolean success;
    //对应信息
    private String msg;
    //数据
    private T data;
    @JSONField(
            format = "yyyy-MM-dd HH:mm:ss"
    )
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private Date time;

    public static ApiResult<Boolean> ok() {
        return ok(true);
    }

    public static <T> ApiResult<T> result(ApiCode apiCode, T data) {
        return result(apiCode, null, data);
    }

    public static <T> ApiResult<T> result(ApiCode apiCode, String message, T data) {
        boolean success = false;
        if (apiCode.getCode() == ApiCode.SUCCESS.getCode()) {
            success = true;
        }

        if (StringUtils.isBlank(message)) {
            message = apiCode.getMessage();
        }

        return (ApiResult<T>) builder().status(apiCode.getCode()).msg(message).data(data).success(success).time(new Date()).build();
    }

    public static <T> ApiResult.ApiResultBuilder<T> builder() {
        return new ApiResult.ApiResultBuilder();
    }

    public static <T> ApiResult<T> ok(T data) {
        return result(ApiCode.SUCCESS, data);
    }

    public static <T> ApiResult<T> ok(T data, String message) {
        return result(ApiCode.SUCCESS, message, data);
    }

    public static ApiResult<Object> fail(ApiCode apiCode) {
        return result(apiCode, null);
    }

    public static ApiResult<String> fail(String message) {
        return result(ApiCode.FAIL, message, null);
    }

    public static class ApiResultBuilder<T> {
        private int status;
        private boolean success;
        private String msg;
        private T data;
        private Date time;

        ApiResultBuilder() {
        }

        public ApiResult.ApiResultBuilder<T> status(final int status) {
            this.status = status;
            return this;
        }

        public ApiResult.ApiResultBuilder<T> success(final boolean success) {
            this.success = success;
            return this;
        }

        public ApiResult.ApiResultBuilder<T> msg(final String msg) {
            this.msg = msg;
            return this;
        }

        public ApiResult.ApiResultBuilder<T> data(final T data) {
            this.data = data;
            return this;
        }

        @JsonFormat(
                pattern = "yyyy-MM-dd HH:mm:ss",
                timezone = "GMT+8"
        )
        public ApiResult.ApiResultBuilder<T> time(final Date time) {
            this.time = time;
            return this;
        }

        public ApiResult<T> build() {
            return new ApiResult(this.status, this.success, this.msg, this.data, this.time);
        }

        public String toString() {
            return "ApiResult.ApiResultBuilder(status=" + this.status + ", success=" + this.success + ", msg=" + this.msg + ", data=" + this.data + ", time=" + this.time + ")";
        }
    }

    public ApiResult(final int status, final boolean success, final String msg, final T data, final Date time) {
        this.status = status;
        this.success = success;
        this.msg = msg;
        this.data = data;
        this.time = time;
    }
}
